package com.atguigu.gmall.list.service.impl;

import com.atguigu.gmall.list.repository.GoodsRepository;
import com.atguigu.gmall.list.service.GoodsService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void saveGoods(Long skuId) {
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId).getData();
        Long category3Id = skuInfo.getCategory3Id();
        BaseCategoryView category3IdView = productFeignClient.getCategoryView(category3Id).getData();
        Long tmId = skuInfo.getTmId();
        BaseTrademark trademark = productFeignClient.getTrademarkById(tmId).getData();
        List<BaseAttrInfo> attrInfoList = productFeignClient.getAttrListBySkuId(skuId).getData();

        Goods goods = new Goods();
        goods.setId(skuId);
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());

        goods.setTmId(tmId);
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());

        goods.setCategory1Id(category3IdView.getCategory1Id());
        goods.setCategory2Id(category3IdView.getCategory2Id());
        goods.setCategory3Id(category3IdView.getCategory3Id());
        goods.setCategory1Name(category3IdView.getCategory1Name());
        goods.setCategory2Name(category3IdView.getCategory2Name());
        goods.setCategory3Name(category3IdView.getCategory3Name());

        List<SearchAttr> searchAttrList = new ArrayList<>();
        for (BaseAttrInfo baseAttrInfo : attrInfoList) {
            SearchAttr searchAttr = new SearchAttr();
            searchAttr.setAttrId(baseAttrInfo.getId());
            searchAttr.setAttrName(baseAttrInfo.getAttrName());
            searchAttr.setAttrValue(baseAttrInfo.getAttrValueList().get(0).getValueName());
            searchAttrList.add(searchAttr);
        }
        goods.setAttrs(searchAttrList);

        goodsRepository.save(goods);
    }

    @Override
    public void removeGoods(Long skuId) {
        goodsRepository.deleteById(skuId);
    }

    @Override
    public void incrGoodsHotScore(Long skuId) {
        String hotKey = "hot:sku:key";
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        Double IncyHot = zSetOperations.incrementScore(hotKey, skuId, 1);
        if (IncyHot % 10 == 0) {
            Optional<Goods> goods = goodsRepository.findById(skuId);
            Goods goods1 = goods.get();
            goods1.setHotScore(IncyHot.longValue());
            goodsRepository.save(goods1);
        }
    }
}
